package cj.web.tb.controllers;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.constants.Constants;
import cj.web.admin.dao.ConfigInfoDAO;
import cj.web.admin.dao.SysInforDAO;
import cj.web.admin.dao.UserInfoDAO;
import cj.web.admin.domain.ConfigInfo;
import cj.web.admin.domain.SysInfor;
import cj.web.admin.domain.UserInfo;
import cj.web.login.controllers.BaseController;
import cj.web.tb.constants.RaterTypeEnum;
import cj.web.tb.constants.StatusEnum;
import cj.web.tb.dao.GameDAO;
import cj.web.tb.dao.TBRaterRegistrationDAO;
import cj.web.tb.dao.TBRecommendationDAO;
import cj.web.tb.dao.TBRecommendationItemDAO;
import cj.web.tb.dao.TBSchoolRegistrationDAO;
import cj.web.tb.domain.TBRaterRegistration;
import cj.web.tb.domain.TBRecommendation;
import cj.web.tb.domain.TBRecommendationItem;
import cj.web.tb.domain.TBSchoolRegistration;

@Path("/tBRegAudit")
public class TBRegAuditController extends BaseController{
	
	private static String CODE_RATER_AUDIT_OK = "CODE_RATER_AUDIT_OK";//专家审核通过
	private static String CODE_RATER_AUDIT_NO = "CODE_RATER_AUDIT_NO";//专家审核拒绝
	
	private static String CODE_SCHOOL_REG_AUDIT_OK = "CODE_SCHOOL_REG_AUDIT_OK";//学校实名推荐审核通过
	private static String CODE_SCHOOL_REG_AUDIT_NO = "CODE_SCHOOL_REG_AUDIT_NO";//学校实名推荐审核拒绝
	
	private static String CODE_SCHOOL_PEOPLE_AUDIT_OK = "CODE_SCHOOL_PEOPLE_AUDIT_OK";//学校实名推荐人员审核通过
	private static String CODE_SCHOOL_PEOPLE_AUDIT_NO = "CODE_SCHOOL_PEOPLE_AUDIT_NO";//学校实名推荐人员审核拒绝

	private final static String MSG_1="状态不是已申报！";
	
	private static Logger logger = Logger.getLogger(TBRegAuditController.class.getName());
	
	@Autowired
	private TBRaterRegistrationDAO tBRaterRegistrationDao;
	@Autowired
	private TBSchoolRegistrationDAO tBSchoolRegistrationDao;
	@Autowired
	private UserInfoDAO userInfoDao;
	@Autowired
	private TBRecommendationDAO tBRecommendationDao;
	@Autowired
	private TBRecommendationItemDAO tBRecommendationItemDao;
	@Autowired
	private GameDAO gameDao;
	@Autowired
	private SysInforDAO sysInforDao;
	@Autowired
	private ConfigInfoDAO configInfoDao;
	//*******************************************Rater
	@Get("showRater")
	public String showRater(Model model,TBRaterRegistration tBRaterRegistration) {
		return "/views/tb/raterRegAuditList.vm";
	}

	@Post("getRaterList")
	public String queryRaterList(TBRaterRegistration tBRaterRegistration) {
		//当前页  
		tBRaterRegistration.setStart((tBRaterRegistration.getPage()-1)*tBRaterRegistration.getRows());
		tBRaterRegistration.setLength(tBRaterRegistration.getRows());
		
		JSONObject jo = new JSONObject();
		
		List<TBRaterRegistration> objs = tBRaterRegistrationDao.query(tBRaterRegistration);
		
		for(TBRaterRegistration obj:objs){
			obj.setTypeName(RaterTypeEnum.getName(obj.getType()));
			obj.setStatusName(StatusEnum.getName(obj.getStatus()));
		}
		
		int size = tBRaterRegistrationDao.querySize(tBRaterRegistration);
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Get("raterInfor")
	public String raterInfor(Model model,@Param("id") int id)
	{
		TBRaterRegistration obj = tBRaterRegistrationDao.queryById(id);
		obj.setTypeName(RaterTypeEnum.getName(obj.getType()));
		UserInfo userInfo = userInfoDao.queryById(obj.getUserId());
		
		model.add("typelis",EnumSet.allOf(RaterTypeEnum.class));
		model.add("obj", obj);
		model.add("userInfo", userInfo);
		return "/views/tb/raterInfor.vm";
	}
	
	@Get("auditOk")
	public String auditOk(Model model,@Param("Ids") String Ids,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			
			TBRaterRegistration obj = tBRaterRegistrationDao.queryById(Integer.parseInt(id));
			if(obj!=null){
				tBRaterRegistrationDao.updateStatus(Integer.parseInt(id), 
						StatusEnum.TYPE2.getValue(), returnSessionObj(request).getAccount());
				writeSysInfor(CODE_RATER_AUDIT_OK, obj.getUserId());
			}
			
		}
		
		return "@"+jo.toString();
	}
	
	@Get("auditNo")
	public String auditNo(Model model,@Param("Ids") String Ids,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			
			TBRaterRegistration obj = tBRaterRegistrationDao.queryById(Integer.parseInt(id));
			if(obj!=null){
				tBRaterRegistrationDao.updateStatus(Integer.parseInt(id), 
						StatusEnum.TYPE3.getValue(), returnSessionObj(request).getAccount());
				writeSysInfor(CODE_RATER_AUDIT_NO, obj.getUserId());
			}
		}
		
		return "@"+jo.toString();
	}
	
	@Get("updateUserState")
	public String updateUserForState(Model model,@Param("Ids") String Ids,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			TBRaterRegistration obj = tBRaterRegistrationDao.queryById(Integer.parseInt(id));
			userInfoDao.updateState(obj.getUserId());
		}
		
		return "@"+jo.toString();
	}
	
	//*******************************************School
	@Get("showSchool")
	public String showSchool(Model model,TBRaterRegistration tBRaterRegistration) {
		return "/views/tb/schoolRegAuditList.vm";
	}

	@Post("getSchoolList")
	public String querySchoolList(TBSchoolRegistration tBSchoolRegistration) {
		//当前页  
		tBSchoolRegistration.setStart((tBSchoolRegistration.getPage()-1)*tBSchoolRegistration.getRows());
		tBSchoolRegistration.setLength(tBSchoolRegistration.getRows());
		
		JSONObject jo = new JSONObject();
		
		List<TBSchoolRegistration> objs = tBSchoolRegistrationDao.query(tBSchoolRegistration);
		
		for(TBSchoolRegistration obj:objs){
			obj.setStatusName(StatusEnum.getName(obj.getStatus()));
		}
		
		int size = tBSchoolRegistrationDao.querySize(tBSchoolRegistration);
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Get("schoolRegInfor")
	public String schoolRegInfor(Model model,@Param("id") int id)
	{
		TBSchoolRegistration obj = tBSchoolRegistrationDao.queryById(id);
		
		UserInfo userInfo = userInfoDao.queryById(obj.getUserId());
		
		model.add("obj", obj);
		model.add("userInfo", userInfo);
		return "/views/tb/schoolRegInfor.vm";
	}
	
	@Get("auditSchoolOk")
	public String auditSchoolOk(Model model,@Param("Ids") String Ids,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			tBSchoolRegistrationDao.updateStatus(Integer.parseInt(id), 
					StatusEnum.TYPE2.getValue(), returnSessionObj(request).getAccount());
		}
		
		return "@"+jo.toString();
	}
	
	@Get("auditSchoolNo")
	public String auditSchoolNo(Model model,@Param("Ids") String Ids,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			tBSchoolRegistrationDao.updateStatus(Integer.parseInt(id), 
					StatusEnum.TYPE3.getValue(), returnSessionObj(request).getAccount());
		}
		
		return "@"+jo.toString();
	}
	
	@Get("updateSchoolState")
	public String updateSchoolForState(Model model,@Param("Ids") String Ids,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			TBSchoolRegistration obj = tBSchoolRegistrationDao.queryById(Integer.parseInt(id));
			userInfoDao.updateState(obj.getUserId());
		}
		
		return "@"+jo.toString();
	}
	
	//*******************************************recommendation
	
	/**
	 * 学校申报审核
	 * @param model
	 * @param tBRecommendation
	 * @return
	 */
	@Get("showRecDeclare")
	public String showRecDeclare(Model model,TBRecommendation tBRecommendation) {
		List<Map> gamelis =  gameDao.queryAllForEndDate();
		model.add("gamelis", gamelis);
		return "/views/tb/recDeclareAuditList.vm";
	}
	
	/**
	 * 申报人员审核
	 * @param model
	 * @param tBRecommendation
	 * @return
	 */
	@Get("showRecommendation")
	public String showRecommendation(Model model,TBRecommendation tBRecommendation) {
		List<Map> gamelis =  gameDao.queryAllForEndDate();
		model.add("gamelis", gamelis);
		return "/views/tb/recommendationAuditList.vm";
	}
	
	/**
	 * 审核通过
	 * @param model
	 * @param id
	 * @param request
	 * @return
	 */
	@Get("auditRecommendationOk")
	public String auditRecommendationOk(Model model,@Param("id") int id,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		TBRecommendation obj = tBRecommendationDao.queryById(id);
		
		if(StatusEnum.TYPE1.getValue()==obj.getStatus()){
			tBRecommendationDao.updateStatus(id, StatusEnum.TYPE2.getValue(), returnSessionObj(request).getAccount(),"");
			writeSysInfor(CODE_SCHOOL_REG_AUDIT_OK, obj.getUserId());
		}else{
			//A003:状态不符合
			jo.put("returnFlag", Constants.returnObj.returnCode.code_3);
		}
		
		return "@"+jo.toString();
	}
	
	/**
	 * 审核拒绝
	 * @param model
	 * @param id
	 * @param request
	 * @return
	 */
	@Get("auditRecommendationNo")
	public String auditRecommendationNo(Model model,@Param("id") int id,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		TBRecommendation obj = tBRecommendationDao.queryById(id);
		
		if(StatusEnum.TYPE4.getValue()!=obj.getStatus() 
				&& StatusEnum.TYPE5.getValue()!=obj.getStatus() 
				&& StatusEnum.TYPE6.getValue()!=obj.getStatus() ){
			tBRecommendationDao.updateStatus(id, StatusEnum.TYPE3.getValue(), returnSessionObj(request).getAccount(),"");
			writeSysInfor(CODE_SCHOOL_REG_AUDIT_NO,obj.getUserId());
		}else{
			//A003:状态不符合
			jo.put("returnFlag", Constants.returnObj.returnCode.code_3);
		}
		
		return "@"+jo.toString();
	}
	
	/**
	 * 提交申报
	 * @param model
	 * @param Ids
	 * @param request
	 * @return
	 */
	@Get("auditRecommendationRec")
	public String auditRecommendationRec(Model model,@Param("id") int id,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		TBRecommendation obj = tBRecommendationDao.queryById(id);
		
		TBRecommendationItem tBRecommendationItem = new TBRecommendationItem();
		tBRecommendationItem.setRecommendationId(id);
		int ItemSize = tBRecommendationItemDao.querySize(tBRecommendationItem);
		
		if(ItemSize==0){
			//A002:项次为空
			jo.put("returnFlag", Constants.returnObj.returnCode.code_2);
			return "@"+jo.toString();
		}
		
		if(StatusEnum.TYPE2.getValue()==obj.getStatus() || StatusEnum.TYPE6.getValue()==obj.getStatus()){
			tBRecommendationDao.updateStatus(id, StatusEnum.TYPE4.getValue(), returnSessionObj(request).getAccount(),"");
		}else{
			//A003:状态不符合
			jo.put("returnFlag", Constants.returnObj.returnCode.code_3);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("showRecAudit")
	public String showRecAudit(Model model,@Param("id") int id,HttpServletRequest request) {
		
		//查询提交申报信息
		TBRecommendation tBRecommendationobj = tBRecommendationDao.queryById(id);
		
		if(tBRecommendationobj.getStatus()!=StatusEnum.TYPE4.getValue()){
			model.add("msg",MSG_1);
			return "/views/tb/error.vm";
		}
		
		//查询提交申报学校信息
		TBSchoolRegistration tBSchoolRegistrationObj = 
				tBSchoolRegistrationDao.queryByUserId(tBRecommendationobj.getUserId());
		model.add("tBSchoolRegistrationObj", tBSchoolRegistrationObj);
		model.add("id", id);
		
		return "/views/tb/recommendationRecAudit.vm";
	}
	
	/**
	 * 申报通过
	 * @param model
	 * @param Ids
	 * @param request
	 * @return
	 */
	@Post("auditRecommendationRecOk")
	public String auditRecommendationRecOk(Model model,@Param("id") int id,
			@Param("remark") String remark,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		TBRecommendation obj = tBRecommendationDao.queryById(id);
		
		if(StatusEnum.TYPE4.getValue()==obj.getStatus()){
			tBRecommendationDao.updateStatus(id, StatusEnum.TYPE5.getValue(), returnSessionObj(request).getAccount(),remark);
			writeSysInfor(CODE_SCHOOL_PEOPLE_AUDIT_OK,obj.getUserId(),remark);
		}else{
			//A003:状态不符合
			jo.put("returnFlag", Constants.returnObj.returnCode.code_3);
		}
		
		return "@"+jo.toString();
	}
	
	/**
	 * 申报通过拒绝
	 * @param model
	 * @param Ids
	 * @param request
	 * @return
	 */
	@Post("auditRecommendationRecNo")
	public String auditRecommendationRecNo(Model model,@Param("id") int id,
			@Param("remark") String remark,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		TBRecommendation obj = tBRecommendationDao.queryById(id);
		
		if(StatusEnum.TYPE1.getValue()!=obj.getStatus() 
				&& StatusEnum.TYPE2.getValue()!=obj.getStatus() 
				&& StatusEnum.TYPE3.getValue()!=obj.getStatus() ){
			tBRecommendationDao.updateStatus(id, StatusEnum.TYPE6.getValue(), returnSessionObj(request).getAccount(),remark);
			writeSysInfor(CODE_SCHOOL_PEOPLE_AUDIT_NO,obj.getUserId(),remark);
		}else{
			//A003:状态不符合
			jo.put("returnFlag", Constants.returnObj.returnCode.code_3);
		}
		
		return "@"+jo.toString();
	}
	
	/**
	 * 写系统消息
	 * @param code
	 */
	private void writeSysInfor(String code,int userId){
		if(code !=null && !"".equals(code)){
			List<ConfigInfo> objs = configInfoDao.queryByCode(code);
			if(objs!=null && objs.size()>0){
				String name = objs.get(0).getName();
				String descs = objs.get(0).getValue();
				SysInfor sysInfor = new SysInfor();
				sysInfor.setCreateUser("sys");
				sysInfor.setName(name);
				sysInfor.setDescs(descs);
				sysInfor.setUserId(userId);
				sysInforDao.create(sysInfor);
			}
		}
	}
	
	/**
	 * 写系统消息
	 * @param code
	 */
	private void writeSysInfor(String code,int userId,String descs){
		if(code !=null && !"".equals(code)){
			List<ConfigInfo> objs = configInfoDao.queryByCode(code);
			if(objs!=null && objs.size()>0){
				String name = objs.get(0).getName();
				SysInfor sysInfor = new SysInfor();
				sysInfor.setCreateUser("sys");
				sysInfor.setName(name);
				sysInfor.setDescs(descs);
				sysInfor.setUserId(userId);
				sysInforDao.create(sysInfor);
			}
		}
	}
}